package com.sinosoft.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.base.BaseFormController;
import com.sinosoft.base.Constants;
import com.sinosoft.report.Item;
import com.sinosoft.report.ReportComments;
import com.sinosoft.report.ReportManager;
import com.sinosoft.report.ReportView;
import com.sinosoft.util.CheckEmpty;
import com.sinosoft.util.PropertiesUtil;

@SuppressWarnings("unchecked")
@Controller
public class UserReportController extends BaseFormController {

	private ReportManager reportMan;

	@Autowired
	public void setReportManager(ReportManager reportMan) {
		this.reportMan = reportMan;
	}

	/**
	 * 用户数量统计
	 * 
	 * @param model
	 * @param vo
	 *            接受页面参数值
	 * @param request
	 * @param response
	 * @return 用户数量列表集合
	 */
	@RequestMapping("report/usercount.html")
	protected ModelAndView countUserList(Model model, ReportView vo,
			HttpServletRequest request, HttpServletResponse response) {
		List list = null;
		try {
			// 解析分组条件和排序条件
			String group = ReportComments.countStr(vo.getGroup(), ",");
			String order = ReportComments.filterOrder(vo.getGroup(), vo
					.getOrder(), ",");
			vo.setStrgroup(group);
			vo.setStrorder(order);
			// 查询结果
			list = reportMan.searchUser(vo);
			// 判断结果是否有数据
			if (null != list && list.size() > 0) {
				model.addAttribute("listresult", true);
			} else {
				model.addAttribute("listresult", false);
			}
			// 初始化页面参数
			// 应用领域
			String[] territory = PropertiesUtil.getPropertiesValue("territory",
					request);
			List<Item> territorylist = ReportComments.getList(territory);
			model.addAttribute("territorylist", territorylist);

			// 用户等级
			String[] grade = PropertiesUtil.getPropertiesValue(
					"count.grade.info", request);
			List<Item> gradelist = ReportComments.getList(grade);
			model.addAttribute("gradelist", gradelist);
			// 记录参数
			model.addAttribute("reportview", vo);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView("/report/userreport",
				Constants.USER_COUNT_LIST, list);
	}

	@RequestMapping("report/userreport.html")
	protected ModelAndView countUserLists(String starttime, String endtime,
			String stry, HttpServletRequest request,
			HttpServletResponse response) {
		String group = stry;
		if (CheckEmpty.isEmpty(stry) || stry.equals("null") || stry.equals("")) {
			group = "territory";
		}

		List<ReportView> list = reportMan.searchUser(group, starttime, endtime);
		Map<String, Integer> map = new HashMap<String, Integer>();
		int count=0;
		for (int i = 0; i < list.size(); i++) {
			ReportView vo = list.get(i);
			count+=vo.getNum();
			map.put(vo.getValue(), vo.getNum());
		}

		JSONObject jsonobj = new JSONObject();
		jsonobj.put("ingredients", map);
		jsonobj.put("count", count);

		try {
			response.getWriter().write(jsonobj.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("setuserreport.html")
	protected String setcounturl(HttpServletRequest request,
			HttpServletResponse response) {
		return "report/userreport";
	}

}
